package com.yklx.youke.controller;

import com.yklx.youke.service.YoubiTransactionService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/payment")
@CrossOrigin(origins = "*")
public class PaymentController {

    private final YoubiTransactionService transactionService;

    public PaymentController(YoubiTransactionService transactionService) {
        this.transactionService = transactionService;
    }

    @GetMapping("/youbi/balance")
    public ResponseEntity<Map<String, Object>> getBalance() {
        Map<String, Object> balance = transactionService.getBalance("currentUser"); // In a real implementation, get from security context
        return ResponseEntity.ok(balance);
    }

    @PostMapping("/youbi/deposit")
    public ResponseEntity<?> deposit(@RequestBody Map<String, Object> requestData) {
        String userId = (String) requestData.get("userId");
        Double amount = Double.parseDouble(requestData.get("amount").toString());
        String paymentMethod = (String) requestData.get("paymentMethod");
        String externalRef = (String) requestData.get("externalPaymentRef");

        try {
            var transaction = transactionService.deposit(userId, amount, paymentMethod, externalRef);
            return ResponseEntity.ok(Map.of(
                "transaction", transaction,
                "newBalance", transactionService.getBalance(userId)
            ));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    @GetMapping("/youbi/transactions")
    public ResponseEntity<?> getTransactions(
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String dateRange,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer limit) {
        
        // In a real implementation, this would fetch user transactions
        return ResponseEntity.ok(Map.of(
            "transactions", new Object[]{},
            "total", 0,
            "page", page != null ? page : 1
        ));
    }

    @PostMapping("/youbi/transfer")
    public ResponseEntity<?> transfer(@RequestBody Map<String, Object> requestData) {
        String fromUserId = (String) requestData.get("fromUserId");
        String toUserId = (String) requestData.get("toUserId");
        Double amount = Double.parseDouble(requestData.get("amount").toString());
        String description = (String) requestData.get("description");

        try {
            // In a real implementation, this would be restricted to assistants for distribution
            var transaction = transactionService.processPayment(fromUserId, toUserId, amount, description);
            return ResponseEntity.ok(Map.of(
                "transaction", transaction,
                "newBalance", transactionService.getBalance(fromUserId)
            ));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }
}